package com.hanson.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.UserBean;
import com.hanson.service.OrderService;
import com.hanson.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String login(ModelMap modelMap, String pid) {
        return "admin/user/index";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(ModelMap modelMap, String id) {
        modelMap.put("data", userService.selectById(id));
        return "admin/user/modify";
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(ModelMap modelMap, String id) {
        UserBean userBean = userService.selectById(id);
        modelMap.put("data", userBean);
        if (null != userBean) {
            modelMap.put("weekData", orderService.countByOpenId(userBean.getOpenId(), "week"));
            modelMap.put("allData", orderService.countByOpenId(userBean.getOpenId(), null));
        }
        return "admin/user/detail";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modify(UserBean userBean) {
        ResultMessage resultMessage = new ResultMessage();
        userService.updateUser(userBean);
        resultMessage.setMsg("修改用户成功！");
        return resultMessage;
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<UserBean> listData(int limit, int page, UserBean userBean) {
        Page<UserBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(userService.listUsers(pageInfo, userBean));
    }
}
